package com.zpark.mapper;

import com.zpark.pojo.MyUser;
import com.zpark.pojo.User;
import com.zpark.pojo.vo.UserToAccount;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * @PackageName: com.zpark.mapper
 * @ClassName: IUserMapper
 * @Description:
 * @author: RZS
 * @date: 2023/4/24  14:36
 */
// mybatis 基于注解方式实现配置二级缓存
@CacheNamespace(blocking = true)
public interface IUserMapper {

    // 新增用户数据
    @Insert("insert into user(username, password, sex, language, city) " +
            "values(#{username}, #{password}, #{sex}, #{language}, #{city})")
    // 返回新增数据的ID
    @SelectKey(keyProperty = "id", keyColumn = "id", before = false, resultType = Integer.class,
    statement = "select last_insert_id()")
    Integer insertUser(User user);

    // 根据用户id删除用户数据
    @Delete("delete from user where id=#{uid}")
    Integer deleteUserById(Integer uid);

    // 根据用户id修改用户数据
    @Update("update user set username=#{username}, password=#{password}," +
            " sex=#{sex}, language=#{language}, city=#{city}" +
            " where id=#{id}")
    Integer updateUserById(User user);

    // 查询所有用户数据
    @Select("select id, username, password, sex, language, city from user")
    List<User> selectAll();

    // 根据ID查询用户数据
    @Select("select id, username, password, sex, language, city from user where id=#{uid}")
    User selectUserById(Integer uid);

    // 根据用户名字模糊查询用户数据
    @Select("select id, username, password, sex, language, city from user where username like #{likeName}")
    List<User> selectUserByLikeUname(String likeName);

    // 查询用户数据（字段与属性对应不上）
    @Select("select id, username, password, sex, language, city from user")
    @Results(id = "selMyUser", value = {
            @Result(id = true, property = "uid", column = "id"),
            @Result(property = "uName", column = "username"),
            @Result(property = "pwd", column = "password"),
            @Result(property = "gender", column = "sex"),
            @Result(property = "language", column = "language"),
            @Result(property = "city", column = "city")
    })
    List<MyUser> selectAllMyUser();

    @Select("select id, username, password, sex, language, city from user where id=#{mid}")
    @ResultMap("selMyUser")
    MyUser selectByIdMyUser(Integer mid);

    @Select("select id, username, password, sex, language, city from user")
    @Results(id = "userToAccount", value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "sex", column = "sex"),
            @Result(property = "language", column = "language"),
            @Result(property = "city", column = "city"),

            @Result(property = "accounts", column = "id",
            many = @Many(
                    select = "com.zpark.mapper.IAccountMapper.selectById",
                    fetchType = FetchType.DEFAULT
            ))
    })
    List<UserToAccount> selectUserToAccount();

}
